package pkg16b_eca.SQL.Tables;

import pkg16b_eca.SQL.Query;
import pkg16b_eca.data.validate.Text;
import se.oru.informatik.InformatikException;

public class SQLRoll_Kraver_Formaga extends Query {
    
    public SQLRoll_Kraver_Formaga() throws InformatikException {}
    
    //Insert a förmåga to roller.
    public void insertFormagaToRoll(final int RID, final int FID) {
        //Check so that the förmåga and roller exists before adding them.
        if (rollExists(RID) && formagaExists(FID)) {
            //Insert and execute.
            String query = "insert into roll_kraver_formaga (FID, RID) values (" + FID + ", " + RID + ")";
            SQLInsert(query);
        }
    }
    
    //Insert a förmåga to roller.
    public void insertFormagaToRoll(final String RID, final String FID) {
        if (Text.parseNumber(RID + FID) > -1) {
            insertFormagaToRoll(Text.parseNumber(FID), Text.parseNumber(RID));
        }
    }
    
    //Remove a förmåga from roller
    public void deleteFormagaFromRoll(final int RID, final int FID) {
        //Insert and execute.
        String query = "delete from roll_kraver_formaga where FID=" + FID + " and RID=" + RID;
        SQLDelete(query);
    }
    
    /**
     * Checks if a förmåga exists in the database.
     */
    private boolean formagaExists(int FID) {
        String query = SQLSelect("select FID from formaga where FID = '" + FID + "'");
        return !query.isEmpty();
    }
    
    /**
     * Checks if a roll exists in the database.
     */
    public boolean rollExists(final int RID) {
        String query = SQLSelect("select RID from roll where RID = '" + RID + "'");
        return !query.isEmpty();
    }
    
    //return true if the combination of the RID and RID exists in the database roll_kraver_formaga.
        public boolean exists(final int RID, final int FID) {
        return !SQLSelect("select RID from roll_kraver_formaga where RID=" + RID + " and FID=" + FID).isEmpty();
    }
    
    //return true if the combination of the RID and FID exists in the database roll_kraver_formaga.
    public boolean exists(final String RID, final String FID) {
        if (Text.parseNumber(RID) >= 0 && Text.parseNumber(FID) >= 0) {
            return exists(RID, FID);
        }
        return false;
    }
}
